//
//  AkuWeaTherView.m
//  AkuCareLink
//
//  Created by 阿酷 on 16/7/5.
//  Copyright © 2016年 AkuApp. All rights reserved.
//

#import "AkuWeaTherView.h"
#import "AkuWeatherModel.h"
#import <UIImageView+WebCache.h>

#define AKuTextColor [UIColor colorWithRed:105/255.0 green:218/255.0 blue:207/255.0 alpha:1]

@interface AkuWeaTherView ()

@property (nonatomic, weak) UIImageView *dayPictureImageView;

@property (nonatomic, weak) UILabel *weatherText;

@property (nonatomic, weak) UIButton *pictCityButton;

@property (nonatomic, weak) UILabel *temperatureText;

@property (nonatomic, weak) UILabel *dateText;

@end

@implementation AkuWeaTherView

- (instancetype)init {

    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}

#pragma mark - 创建控件
- (void)setupUI {
    
    // 天气 图片
    UIImageView *dayPictureImageView = [[UIImageView alloc] init];
    
    self.dayPictureImageView = dayPictureImageView;
    
    // 设置 圆角属性.bd提供的 图片边角 有锯齿 效果
    dayPictureImageView.layer.cornerRadius = 10;
    dayPictureImageView.layer.masksToBounds= YES;  //设置圆角必须设置此属性
    
    [self addSubview:dayPictureImageView];
    
    // 天气 信息 (例如:大雨)
    UILabel *weatherText = [[UILabel alloc] init];
    
    self.weatherText = weatherText;
    weatherText.textColor = AKuTextColor;
    weatherText.font = [UIFont systemFontOfSize:14];
    
    // 行数 设置 为 0 因为 天气 字数 不确定  多了 就需要换行
    weatherText.numberOfLines = 0;
    weatherText.textColor = AKuTextColor;
    
    [self addSubview:weatherText];
    
    // 天气选择 Button
    UIButton *pictCityButton = [[UIButton alloc] init];
    
    self.pictCityButton = pictCityButton;

    pictCityButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [self addSubview:pictCityButton];
    
    // 温度
    UILabel *temperatureText = [[UILabel alloc] init];
    
    self.temperatureText = temperatureText;
    
    temperatureText.font = [UIFont systemFontOfSize:14];
    temperatureText.textColor = AKuTextColor;
    // 居中显示
    temperatureText.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:temperatureText];
    // 日期
    UILabel *dateText = [[UILabel alloc] init];
    
    self.dateText = dateText;
    dateText.textColor = AKuTextColor;
    
    dateText.font = [UIFont systemFontOfSize:14];
    
    // 居中显示
    dateText.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:dateText];
    
    // 添加 button 的点击事件
    [pictCityButton addTarget:self action:@selector(didPictCityButton:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 添加约束
- (void)setViewMasonry {
    
    /*因为 每次 获取 到天气 之后的 的 城市文字  跟 天气 文字 字数  都会 不同,所以 每次获取 到信息之后 还需要 更新 约束*/

    // 天气 图片 约束
    [self.dayPictureImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mas_centerY);
        make.left.offset(10);
        make.width.height.offset(50);
    }];
    
    // 天气选择 Button
    [self.pictCityButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_centerX).offset(-10);
        make.size.offset(self.pictCityButton.bounds.size);
    }];
    
    // 天气 信息 (例如:大雨) 约束
    [self.weatherText mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.dayPictureImageView.mas_right).offset(10);
        
        // 右边 跟着 挨着 button
        make.right.equalTo(self.pictCityButton.mas_left).offset(-5);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    // 温度
    [self.temperatureText mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_centerY).offset(-25);
        make.left.equalTo(self.mas_centerX);
        make.right.offset(0);
    }];
    
    // 日期
    [self.dateText mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.mas_centerY).offset(25);
        make.left.equalTo(self.mas_centerX);
        make.right.offset(0);
    }];
    
    // 中间的分割线
    UIView *dividingLineView = [[UIView alloc] init];
    
    [self addSubview:dividingLineView];
    
    dividingLineView.backgroundColor = [UIColor grayColor];
    
    [dividingLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.mas_centerX).offset(-0.5);
        make.top.offset(10);
        make.bottom.offset(-10);
        make.width.offset(1);
    }];
    
}

#pragma mark - 给控件 添加文字信息
- (void)setWeatherModel:(AkuWeatherModel *)weatherModel {
    
    _weatherModel = weatherModel;
    
    // 天气图片
    [self.dayPictureImageView sd_setImageWithURL:[NSURL URLWithString:weatherModel.dayPictureUrl]];
    
    // 天气详情
    self.weatherText.text = weatherModel.weather;
    
    // 温度
    self.temperatureText.text = [NSString stringWithFormat:@"温度: %@", weatherModel.temperature];
    
    // 日期   原文:  --->周二 07月05日 (实时：33℃)<---  一共18 个  我只需要 前面 9个 --> 周二 07月05日  所以需要截取
    if (weatherModel.date.length > 9) {
        
        weatherModel.date = [weatherModel.date substringWithRange: NSMakeRange(0, 9)];
    }
    
    self.dateText.text = [NSString stringWithFormat:@"日期: %@", weatherModel.date];
    
    // 天气 城市 选择
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:weatherModel.currentCity];
    
    NSRange strRange = {0,[str length]};
    
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    // 文字及下划线 颜色
    [str addAttribute:NSForegroundColorAttributeName value:AKuTextColor range:strRange];
    
    [self.pictCityButton setAttributedTitle:str forState:UIControlStateNormal];
    
    //MARK: - 宽高 根据文字 自适应
    [self.pictCityButton sizeToFit];
    
    [self setViewMasonry];
    
}

#pragma mark - 点击button的方法
- (void)didPictCityButton:(UIButton *)sender {
    
    // 点击 pictCityButton 通过 block 回调 跳转到 选择城市 界面
    if (self.didPictCityButton) {

        self.didPictCityButton();
    }
}


@end

















